/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
// ADAPTED FROM:
/**
 * EditPolicy for the Figure used by this edit part.
 * Children of XYLayoutEditPolicy can be used in Figures with XYLayout.
 * @author Elias Volanakis
 */

package org.apache.pig.pigpen.editor.policies;

import org.apache.pig.pigpen.editor.commands.OperatorCreateCommand;
import org.apache.pig.pigpen.editor.model.CogroupOperator;
import org.apache.pig.pigpen.editor.model.FilterOperator;
import org.apache.pig.pigpen.editor.model.ForeachOperator;
import org.apache.pig.pigpen.editor.model.GroupOperator;
import org.apache.pig.pigpen.editor.model.LoadOperator;
import org.apache.pig.pigpen.editor.model.Operator;
import org.apache.pig.pigpen.editor.model.Query;
import org.apache.pig.pigpen.editor.model.StoreOperator;
import org.eclipse.draw2d.FlowLayout;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.LayoutEditPolicy;
import org.eclipse.gef.requests.CreateRequest;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class QueryLayoutEditPolicy extends LayoutEditPolicy {

    /*
     * (non-Javadoc)
     * 
     * @see LayoutEditPolicy#getCreateCommand(CreateRequest)
     */
    protected Command getCreateCommand(CreateRequest request) {
        Object childClass = request.getNewObjectType();
        /*
         * if (childClass == Operator.class) { // return a command that can add
         * an Operator to a Query //Get PigLatin statement (not Profit and Loss)
         * StringBuffer str = new StringBuffer(); getPLStatement(str);
         * 
         * Operator newOp = (Operator)request.getNewObject();
         * newOp.setText(str.toString());
         * 
         * return new OperatorCreateCommand(newOp, (Query)getHost().getModel());
         * }
         */
        Operator newOp = null;
        if (childClass == LoadOperator.class) {
            newOp = (LoadOperator) request.getNewObject();
            StringBuffer str = new StringBuffer();
            ((LoadOperator) newOp).GeneratePigLatinStatement(str);
            newOp.setText(str.toString());
        } else if (childClass == ForeachOperator.class) {
            newOp = (ForeachOperator) request.getNewObject();
            StringBuffer str = new StringBuffer();
            ((ForeachOperator) newOp).GeneratePigLatinStatement(str);
            newOp.setText(str.toString());
        } else if (childClass == FilterOperator.class) {
            newOp = (FilterOperator) request.getNewObject();
            StringBuffer str = new StringBuffer();
            ((FilterOperator) newOp).GeneratePigLatinStatement(str);
            newOp.setText(str.toString());
        } else if (childClass == CogroupOperator.class) {
            newOp = (CogroupOperator) request.getNewObject();
            StringBuffer str = new StringBuffer();
            ((CogroupOperator) newOp).GeneratePigLatinStatement(str);
            newOp.setText(str.toString());
        } else if (childClass == GroupOperator.class) {
            newOp = (GroupOperator) request.getNewObject();
            StringBuffer str = new StringBuffer();
            ((GroupOperator) newOp).GeneratePigLatinStatement(str);
            newOp.setText(str.toString());
        } else if (childClass == StoreOperator.class) {
            newOp = (StoreOperator) request.getNewObject();
            StringBuffer str = new StringBuffer();
            ((StoreOperator) newOp).GeneratePigLatinStatement(str);
            newOp.setText(str.toString());
        }
        if (newOp != null)
            return new OperatorCreateCommand(newOp, (Query) getHost()
                    .getModel());
        else
            return null;
        /*
         * if(childClass instanceof Operator) { Operator newOp = null;
         * if(childClass == LoadOperator.class) { newOp = (LoadOperator)
         * request.getNewObject(); StringBuffer str = new StringBuffer();
         * ((LoadOperator)newOp).GeneratePigLatinStatement(str);
         * newOp.setText(str.toString()); } return new
         * OperatorCreateCommand(newOp, (Query)getHost().getModel()); }
         */
        // return null;
    }

    @Override
    protected EditPolicy createChildEditPolicy(EditPart child) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected Command getMoveChildrenCommand(Request request) {
        // TODO Auto-generated method stub
        return null;
    }

    public void getPLStatement(final StringBuffer str) {
        // Display display = new Display();
        Display display = Display.getCurrent();
        // final StringBuffer str = new StringBuffer();
        final Shell shell = new Shell(display);
        shell.setText("Show Message Box");
        // createContents(shell);

        shell.setLayout(new GridLayout(11, false));

        // Create the entry field for the message

        final Text identifier = new Text(shell, SWT.NONE);
        new Label(shell, SWT.NONE).setText(" = LOAD '");
        final Text fileName = new Text(shell, SWT.NONE);
        new Label(shell, SWT.NONE).setText("' USING ");
        final Text loadFunc = new Text(shell, SWT.NONE);
        new Label(shell, SWT.NONE).setText(" AS (");
        final Text specifiedAlias = new Text(shell, SWT.NONE);
        new Label(shell, SWT.NONE).setText(");");
        // message.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        // Create the label to show the return from the open call
        // new Label(shell, SWT.NONE).setText("Return:");
        final Label returnVal = new Label(shell, SWT.NONE);
        returnVal.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        // Create the button and event handler
        // to display the message box
        Button button = new Button(shell, SWT.PUSH);
        button.setText("Add Operator");

        button.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent event) {
                // Clear any previously returned value
                returnVal.setText("");

                str.append(identifier.getText());
                str.append(" = LOAD '");
                str.append(fileName.getText());
                str.append("' USING ");
                str.append(loadFunc.getText());
                str.append(" AS (");
                str.append(specifiedAlias.getText());
                str.append(");");
                shell.dispose();
            }
        });

        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        // display.dispose();
    }

    private void createContents(final Shell shell) {
        shell.setLayout(new GridLayout(11, false));

        // Create the entry field for the message

        final Text identifier = new Text(shell, SWT.NONE);
        new Label(shell, SWT.NONE).setText(" = LOAD '");
        final Text fileName = new Text(shell, SWT.NONE);
        new Label(shell, SWT.NONE).setText("' USING ");
        final Text loadFunc = new Text(shell, SWT.NONE);
        new Label(shell, SWT.NONE).setText(" AS (");
        final Text specifiedAlias = new Text(shell, SWT.NONE);
        new Label(shell, SWT.NONE).setText(");");
        // message.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        // Create the label to show the return from the open call
        // new Label(shell, SWT.NONE).setText("Return:");
        final Label returnVal = new Label(shell, SWT.NONE);
        returnVal.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        // Create the button and event handler
        // to display the message box
        Button button = new Button(shell, SWT.PUSH);
        button.setText("Add Operator");

        button.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent event) {
                // Clear any previously returned value
                returnVal.setText("");

                // Display the message box
                MessageBox mb = new MessageBox(shell, SWT.ICON_INFORMATION);
                mb.setText("Message from SWT");
                String query = identifier.getText() + " = LOAD '"
                        + fileName.getText() + "' USING " + loadFunc.getText()
                        + " AS " + specifiedAlias.getText() + ";";
                mb.setMessage(query);
                int val = mb.open();

            }
        });
    }

}